package com.swyc.osschat.Common.Exception;

import com.swyc.osschat.Common.Util.Object.StrBuilderUtil;
import org.apache.http.util.TextUtils;

import java.util.Collection;

/**
 * @author cydu
 */
public abstract class Asserts {

    public static void check(boolean expression, String message) {
        if (!expression) {
            throw new AssertException(message);
        }
    }

    public static void check(boolean expression, String message, Object... args) {
        if (!expression) {
            throw new AssertException(String.format(message, args));
        }
    }

    public static void check(boolean expression, String message, Object arg) {
        if (!expression) {
            throw new AssertException(String.format(message, arg));
        }
    }

    public static void notNull(Object object, String name) {
        if (object == null) {
            throw new AssertException(name + " is null");
        }
    }

    public static void notEmpty(Collection collection, String name) {
        notNull(collection, name);
        check(!collection.isEmpty(), StrBuilderUtil.concat(name," is empty"));
    }

    public static void noNullElements(Collection collection, String name) {
        notNull(collection, name);
        collection.forEach(col->notNull(col,name));
    }

    public static void notEmpty(CharSequence s, String name) {
        if (TextUtils.isEmpty(s)) {
            throw new AssertException(name + " is empty");
        }
    }

    public static void notBlank(CharSequence s, String name) {
        if (TextUtils.isBlank(s)) {
            throw new AssertException(name + " is blank");
        }
    }

    public static void noNullElements(Object[] array, String name) {
        notNull(array, name);
        for(int i = 0; i < array.length; ++i) {
            notNull(array[i],name);
        }
    }


}
